<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="封面图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coverImg">
              <j-image-upload isMultiple v-model="model.coverImg" :number="1"></j-image-upload>
              <div style="font-size: 12px;color:#999;line-height:0">只能上传一张图片</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="师傅图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workerImg">
              <j-image-upload isMultiple v-model="model.workerImg" :number="9"></j-image-upload>
              <div style="font-size: 12px;color:#999;line-height:0">一次上传可选择多张图片！请一次性上传完所需图片！</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workerName">
              <a-input v-model="model.workerName" placeholder="请输入姓名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workerPhone">
              <a-input v-model="model.workerPhone" placeholder="请输入联系电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="上门时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="visitTime">
              <a-input v-model="model.visitTime" placeholder="请输入上门时间"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="维修范围" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maintainScope">
              <a-textarea v-model="model.maintainScope" rows="4" placeholder="请输入维修范围" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="维修经验" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maintainExperience">
              <a-input v-model="model.maintainExperience" placeholder="请输入维修经验"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="个人介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="introduce">
              <a-textarea v-model="model.introduce" rows="4" placeholder="请输入个人介绍" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="详情介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="describes">
              <j-editor v-model="model.describes"></j-editor>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <!-- <a-input v-model="model.type" placeholder="请输入状态"></a-input> -->
              <a-radio-group v-model="model.type" placeholder="请选择状态">
                <a-radio value="1">审核中</a-radio>
                <a-radio value="2">未通过</a-radio>
                <a-radio value="3">通过</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="失败原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reasons">
              <a-input v-model="model.reasons" placeholder="请输入失败原因"></a-input>
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MachineMaintainWorkerForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        coverImg: [{ required: true, message: '请上传封面图' }],
        workerImg: [{ required: true, message: '请上传师傅图片' }],
        visitTime: [{ required: true, message: '请输入上门时间' }],
        maintainScope: [{ required: true, message: '请输入维修范围' }],
        maintainExperience: [{ required: true, message: '请输入维修经验' }],
        workerName: [{ required: true, message: '请输入姓名' }],
        workerPhone: [{ required: true, pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!' }],
        introduce: [{ required: true, message: '请输入个人介绍' }],
        describes: [{ required: true, message: '请输入详情介绍' }]
      },
      url: {
        add: "/maintainworker/machineMaintainWorker/add",
        edit: "/maintainworker/machineMaintainWorker/edit",
        queryById: "/maintainworker/machineMaintainWorker/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>